package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutEntityService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutOrganizationService;
import com.kingyun.gpsinspection.purificationservice.common.utils.ConstantUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.FileTypeUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.Problem;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.AnalysisProcessService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.IBMBPMService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.TaskLinkService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by liyi on 2016/12/14.
 * 分析处理 ServiceImpl
 */
public class AnalysisProcessServiceImpl implements AnalysisProcessService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());

    // BPM
    @Autowired
    public IBMBPMService ibmbpmService;
    // 分析处理Mapper
    @Autowired
    public AnalysisProcessMapper analysisProcessMapper;
    // 任务环节Service
    @Autowired
    public TaskLinkService taskLinkService;
    // 任务环节Mapper
    @Autowired
    public TaskLinkMapper taskLinkMapper;
    // 工单Mapper
    @Autowired
    public TdmWorkOrderMapper tdmWorkOrderMapper;
    // 问题上报Mapper
    @Autowired
    private TdmProblemMapper tdmProblemMapper;
    // 附件Mapper
    @Autowired
    private TdmFileMapper tdmFileMapper;
    // 问题处置Mapper
    @Autowired
    private DisposeMeasureMapper disposeMeasureMapper;
    @Autowired(required = false)
    private OutEntityService outEntityService;
    @Autowired(required = false)
    public OutOrganizationService outOrganizationService;
    @Autowired
    private OutEmployeeService outEmployeeService;

    /**
     * 保存分析处理信息
     * 注意：解开启事务，不能抛异常
     *
     * @param employeeVo
     * @param analysisProcess
     * @param problem
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public AnalysisProcess saveAnalysisProcess(RedisValue redisValue, EmployeeVo employeeVo, AnalysisProcess analysisProcess, Problem problem, String source, String uploadFiles, String deletedFiles) {
        // 发起流程 调用开启流程（调用BPM开启流程接口）URL 为BPM API方法
        /*String username = "deadmin";
        String password = "deadmin";*/
        //用户名
        String username = redisValue.getLoginName();

        String password = redisValue.getPassWord();
        String result = ibmbpmService.openBPM(employeeVo, (short) 5, username, password, null);
        //如果没有权限直接返回null
        if (result.equals("WorkSpaceError")) {
            return null;
        }
        // 流程实例ID
        String piid = null;
        // 任务节点ID
        String tkiid = null;
        // 任务节点名
        String name = null;
        JSONObject resultObj = JSONObject.parseObject(result);
        // 获取data属性的值
        JSONObject dataObj = (JSONObject) resultObj.get("data");
        //获取tasks属性的值
        JSONArray itemsArray = (JSONArray) dataObj.get("tasks");
        // 循环json数组
        for (int i = 0; i < itemsArray.size(); i++) {
            // 得到json对象
            JSONObject ob = (JSONObject) itemsArray.get(i);
            // name这里是列名称，获取json对象中列名为name的值
            name = ob.getString("name");
            // piid这里是列名称，获取json对象中列名为piid的值
            piid = ob.getString("piid");
            // tkiid这里是列名称，获取json对象中列名为tkiid的值
            tkiid = ob.getString("tkiid");

        }

        // 判断整改通知单下发、问题上报 1：整改通知单下发，0：问题上报
        String param = null;
        try {
            if (source != null && !"".equals(source)) {
                String val = "{'type':" + source + "}";
                param = URLEncoder.encode(val, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        }
        // 完成当前节点，往下一节点扭转
        result = ibmbpmService.completeTask(username, password, tkiid, param);


        //处理返回值为空串：执行了但是没有返回值
        if ("".equals(result)) {
            logger.info("完成分析处理流程第一个任务节点返回值为空串！");
        } else {
            //获得返回值
            resultObj = JSONObject.parseObject(result);
            if (resultObj != null) {
                String status = (String) resultObj.get("status");
                //开始流程成功
                if (status != null && status.equals("200")) {
                    //来源问题：更新问题状态
                    if (source != null && "0".equals(source)) {
                        Problem newProblem = new Problem();
                        newProblem.setProblemId(problem.getProblemId());
                        newProblem.setStatus((short) 2);
                        tdmProblemMapper.updateByPrimaryKeySelective(newProblem);
                    }

                    // 问题为空：添加问题上报信息
                    if (StringUtils.isBlank(problem.getProblemId())) {
                        //设置问题所属组织
                        setOrgIdByCondition(employeeVo, problem);

                        // 设置上报问题ID
                        problem.setProblemId(UniqueUtil.uuid());
                        // 设置创建时间
                        problem.setCreateTime(new Date());
                        // 设置修改时间
                        problem.setModifyTime(new Date());
                        // 设置上报问题创建人ID
                        problem.setCreateUserId(employeeVo.getEmployeeId());
                        // 设置上报问题修改人ID
                        problem.setModifyUserId(employeeVo.getEmployeeId());
                        // 设置上报是否关闭
                        problem.setIsDelete((short) 0);
                        // 更新状态
                        problem.setStatus((short) 2);

                        // 保存上报问题
                        tdmProblemMapper.insert(problem);
                    }
                    // 设置常规操作的任务状态
                    analysisProcess.setTaskStatus((short) 1);
                    // 设置分析处理ID
                    analysisProcess.setAnalysisProcessId(UniqueUtil.uuid());
                    // 设置分析处理上报问题ID
                    analysisProcess.setProblemId(problem.getProblemId());
                    // 设置分析处理创建人ID
                    analysisProcess.setCreateUserId(employeeVo.getEmployeeId());
                    // 设置分析处理创建时间
                    analysisProcess.setCreateTime(new Date());
                    // 设置分析处理修改人ID
                    analysisProcess.setModifyUserId(employeeVo.getEmployeeId());
                    // 设置分析处理修改时间
                    analysisProcess.setModifyTime(new Date());
                    // 状态1是正常；0是删除
                    analysisProcess.setIsDelete((short) 1);
                    // 状态1是启用；0是关闭
                    analysisProcess.setIsEnable((short) 1);
                    // 设置审核状态
                    analysisProcess.setAuditStatus((short) 1);
                    // 任务状态
                    analysisProcess.setTaskStatus((short) 1);
                    // 流程ID
                    analysisProcess.setFlowId(piid);
                    // 保存分析处理
                    analysisProcessMapper.insertSelective(analysisProcess);

                    // 保存附件
                    saveAttach(analysisProcess, uploadFiles, deletedFiles);

                    //创建任务环节对象
                    TaskLink taskLink = new TaskLink();
                    // 设置流程实例ID
                    taskLink.setPiPiid(piid);
                    // 设置任务节点ID
                    taskLink.setTkiid(tkiid);
                    // 设置任务类型
                    taskLink.setTaskType(analysisProcess.getTaskType());
                    // 设置任务ID
                    taskLink.setTaskId(analysisProcess.getAnalysisProcessId());
                    // 设置任务名称
                    taskLink.setTaskLinkName(name);
                    taskLink.setNode(analysisProcess.getNode());
                    // 设置审核状态
                    taskLink.setAuditStatus((short) 1);
                    // 设置ID
                    taskLink.setTaskLinkId(UniqueUtil.uuid());
                    // 设置人员ID
                    taskLink.setUserId(employeeVo.getEmployeeId());
                    // 执行人组织
                    taskLink.setOrgId(employeeVo.getOrgId());
                    // 保存任务环节
                    taskLinkMapper.insertNew(taskLink);
                } else {
                    logger.error("完成分析处理流程第一个任务节点返回状态非200！");
                }
            } else {
                logger.error("完成分析处理流程第一个任务节点返回值非json字符串！");
            }
        }

        return analysisProcess;
    }

    /**
     * 设置问题对象组织
     *
     * @param employeeVo
     * @param problem
     */
    public void setOrgIdByCondition(EmployeeVo employeeVo, Problem problem) {
        //设置问题所属组织
        if (StringUtils.isNotBlank(problem.getProblemObject())) {
            try {
                EntityVo entityVo = outEntityService.getEntityByEntityId(problem.getProblemObject());
                //问题对象ID是生产实体
                if (entityVo != null) {
                    problem.setOrgId(entityVo.getParentOrgId());
                }
                //问题对象ID不是生产实体，而是组织ID
                Organization organization = outOrganizationService.queryOrganization(problem.getProblemObject());
                if (organization != null) {
                    problem.setOrgId(problem.getProblemObject());
                }
                if (entityVo == null && organization == null) {
                    problem.setOrgId(employeeVo.getOrgId());
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        } else {
            problem.setOrgId(employeeVo.getOrgId());
        }
    }

    /**
     * 保存附件
     *
     * @param analysisProcess
     * @param uploadFiles
     * @param deletedFiles
     */
    public int saveAttach(AnalysisProcess analysisProcess, String uploadFiles, String deletedFiles) {
        int a = 0;

        //保存附件
        if (StringUtils.isNotBlank(uploadFiles)) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            //重新设置附件属性
            for (int i = 0; i < listFiles.size(); i++) {
                File _file = listFiles.get(i);
                //设置主键
                _file.setFileId(UniqueUtil.uuid());
                //设置业务数据id
                _file.setBusinessId(analysisProcess.getAnalysisProcessId());
                _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
            }
            //必须判断非空
            if (listFiles != null && listFiles.size() > 0) {
                a += tdmFileMapper.insertByList(listFiles);
            }
        }
        //需要删除的附件信息
        if (StringUtils.isNotBlank(deletedFiles)) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(deletedFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    a += tdmFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }

        return a;
    }

    /**
     * 根据任务流程ID分析处理信息
     *
     * @param piid
     * @return
     * @throws Exception
     */
    @Override
    public AnalysisProcessVo findAnalysisProcessByPiid(String piid) throws Exception {
        // 判断任务流程ID是否为null 若为空则返回null
        if (StringUtils.isBlank(piid)) {
            return null;
        }

        // 根据流程ID获取分析处理LIST信息
        AnalysisProcessVo analysisProcessVo = analysisProcessMapper.findAnalysisProcessByPiid(piid);

        return analysisProcessVo;
    }

    /**
     * 根据分析处理ID修改上报问题信息与分析处理节点
     * 注意：解开启事务，不能抛异常
     *
     * @param analysisProcess
     * @param problem
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateById(AnalysisProcess analysisProcess, Problem problem, TaskLink taskLink) {
        int a = 0;

        // 根据前台表单传的信息判断是否重大变更：如果是重大变更，则结束当前分析处理流程，修改任务状态
        if (analysisProcess.getIsMajorAlteration() != null && analysisProcess.getIsMajorAlteration() == 1) {
            // 根据IsMajorAlteration判断：0代表重大变更
            analysisProcess.setTaskStatus((short) 3);
            analysisProcess.setFinishTime(new Date());
            // 流程处理完成修改任务状态
        } else if (ConstantUtil.TASK_ASSIGN1.equals(taskLink.getTaskLinkName()) || ConstantUtil.TASK_ASSIGN2.equals(taskLink.getTaskLinkName()) || ConstantUtil.TASK_ASSIGN3.equals(taskLink.getTaskLinkName())) {
            analysisProcess.setTaskStatus((short) 3);
            analysisProcess.setFinishTime(new Date());

            // 更新问题状态
            Problem newProblem = new Problem();
            newProblem.setProblemId(problem.getProblemId());
            newProblem.setStatus((short) 3);
            tdmProblemMapper.updateByPrimaryKeySelective(newProblem);
        }
        // 判断是否修改问题整改计划和督办事件
        if (problem != null && problem.getOverseeEvent() != null && problem.getRectificationPlan() != null) {
            a += tdmProblemMapper.updateByPrimaryKeySelective(problem);
        }

        // 修改分析处理信息
        a += analysisProcessMapper.updateByPrimaryKeySelective(analysisProcess);

        return a;
    }

    /**
     * 处理流程：做事务处理
     *
     * @param employeeVo
     * @param analysisProcess
     * @param disposeMeasure
     * @param taskLink        @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int disposeTask(RedisValue redisValue, EmployeeVo employeeVo, AnalysisProcess analysisProcess, DisposeMeasure disposeMeasure, TaskLink taskLink, String uploadFiles, String deletedFiles) {
        // 标志变量
        int a = 0;

        // 流程参数
        String type = null;
        try {
            // 厂领导审核
            if (analysisProcess.getAuditStatus() != null) {
                // 流程走向参数
                String val = "{\"type\":\"" + analysisProcess.getAuditStatus() + "\"}";
                // 转换成utf-8
                type = URLEncoder.encode(val, "utf-8");
                taskLink.setAuditStatus(analysisProcess.getAuditStatus());
            } else {
                // 设置审核状态
                taskLink.setAuditStatus((short) 1);
            }
            // 判断是否为重大变动
            if (analysisProcess.getIsMajorAlteration() != null) {
                // 流程走向参数
                String val = "{\"type\":\"" + analysisProcess.getIsMajorAlteration() + "\"}";
                // 转换成utf-8
                type = URLEncoder.encode(val, "utf-8");
            }
            // 判断是否为重大任务
            if (analysisProcess.getTaskLevel() != null) {
                // 流程走向参数
                String val = "{\"type\":\"" + analysisProcess.getTaskLevel() + "\"}";
                // 转换成utf-8
                type = URLEncoder.encode(val, "utf-8");
            }
            // 判断是否快速处置
            if (analysisProcess.getIsQuickDisposal() != null) {
                // 流程走向参数
                String val = "{\"type\":\"" + analysisProcess.getIsQuickDisposal() + "\"}";
                // 转换成utf-8
                type = URLEncoder.encode(val, "utf-8");
            }

        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        }

        // 用户信息
         /*String username = "deadmin";
        String password = "deadmin";*/
        //用户名
        String username = redisValue.getLoginName();

        String password = redisValue.getPassWord();
        // 完成当前节点，往下一节点扭转
        String result = ibmbpmService.completeTask(username, password, taskLink.getTkiid(), type);

        //处理返回值为空串：执行了但是没有返回值
        if ("".equals(result)) {
            logger.info("完成“" + taskLink.getTaskLinkName() + "”返回值为空串！");
        } else {
            //获得返回值
            JSONObject resultObj = JSONObject.parseObject(result);
            if (resultObj != null) {
                String status = (String) resultObj.get("status");
                //开始流程成功
                if (status != null && status.equals("200")) {
                    // 添加任务环节
                    taskLink.setTaskLinkId(UniqueUtil.uuid());
                    // 设置任务当前节点
                    taskLink.setNode(analysisProcess.getNode());
                    // 设置任务类型
                    taskLink.setTaskType(analysisProcess.getTaskType());
                    // 设置任务ID
                    taskLink.setTaskId(analysisProcess.getAnalysisProcessId());
                    // 执行人ID：当前用户ID
                    taskLink.setUserId(employeeVo.getEmployeeId());
                    // 执行人组织
                    taskLink.setOrgId(employeeVo.getOrgId());
                    // 保存任务环节信息
                    a += taskLinkMapper.insertNew(taskLink);

                    // 设置审核状态
                    analysisProcess.setAuditStatus(taskLink.getAuditStatus());
                    a += analysisProcessMapper.updateByPrimaryKeySelective(analysisProcess);

                    // 保存附件
                    a += saveAttach(analysisProcess, uploadFiles, deletedFiles);

                    //非null才能添加处置建议：
                    if (disposeMeasure != null && disposeMeasure.getMeasureSuggest() != null) {
                        disposeMeasure.setMeasureId(UniqueUtil.uuid());
                        disposeMeasure.setProblemId(analysisProcess.getProblemId());
                        // 设置创建人ID
                        disposeMeasure.setCreateUserId(employeeVo.getEmployeeId());
                        // 设置修改人ID
                        disposeMeasure.setModifyUserId(employeeVo.getEmployeeId());
                        // 状态1是正常；0是删除
                        disposeMeasure.setIsDelete((short) 1);
                        //1问题和处置措施模块提出的处置措施；2分析处理提出的处置措施
                        disposeMeasure.setSourceValue((short) 2);
                        // 保存处置措施管理信息
                        a += disposeMeasureMapper.insertNew(disposeMeasure);

                        // 分析处理回写处置建议ID
                        analysisProcess.setMeasureId(disposeMeasure.getMeasureId());
                        a += analysisProcessMapper.updateByPrimaryKeySelective(analysisProcess);
                    }
                    if (!analysisProcess.getApproveId().equals("") && analysisProcess.getApproveId() != null) {
                        Employee employee = outEmployeeService.findById(analysisProcess.getApproveId());
                        // 根据任务id查询下一节点任务标识
                        String nextTkiid = nextTkiid(employeeVo, taskLink.getPiPiid());
                        if (StringUtils.isEmpty(nextTkiid)) {
                            throw new NullPointerException("任务标识为空");
                        }
                        // 指定下一节点处理人
                        String s3 = null;
                        try {
                            s3 = ibmbpmService.assignTask(nextTkiid, URLEncoder.encode(employee.getLoginId(), "utf-8"), null);
                        } catch (UnsupportedEncodingException e) {
                            logger.error("登录名转换失败");
                        }
                        resultObj = JSONObject.parseObject(s3);
                        String assign2Status = (String) resultObj.get("status");
                        if (assign2Status != null && !"200".equals(assign2Status)) {
                            logger.error("指定审批人失败");
                        }
                    }
                    if (analysisProcess.getAuditStatus() == 0 && ConstantUtil.TASK_ASSIGN3.equals(taskLink.getTaskLinkName())) {
                        TaskLink link = new TaskLink();
                        link.setTaskId(analysisProcess.getAnalysisProcessId());
                        link.setTaskLinkName(taskLink.getTaskLinkName());
                        // 主办单位任务分解处理人
                        String userId = taskLinkMapper.findTaskLink(link).get(0).getUserId();
                        Employee employee = outEmployeeService.findById(userId);
                        // 根据任务id查询下一节点任务标识
                        String nextTkiid = nextTkiid(employeeVo, link.getPiPiid());
                        if (StringUtils.isEmpty(nextTkiid)) {
                            throw new NullPointerException("任务标识为空");
                        }
                        // 指定下一节点处理人
                        String s3 = null;
                        try {
                            s3 = ibmbpmService.assignTask(nextTkiid, URLEncoder.encode(employee.getLoginId(), "utf-8"), null);
                        } catch (UnsupportedEncodingException e) {
                            logger.error("登录名转换失败");
                        }
                        resultObj = JSONObject.parseObject(s3);
                        String assign2Status = (String) resultObj.get("status");
                        if (assign2Status != null && !"200".equals(assign2Status)) {
                            logger.error("指定审批人失败");
                        }
                    }
                } else {
                    logger.error("完成“" + taskLink.getTaskLinkName() + "”返回状态非200！");
                }
            } else {
                logger.error("完成“" + taskLink.getTaskLinkName() + "”返回值非json字符串！");
            }
        }

        return a;
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Override
    public AnalysisProcessVo queryByPrimaryKey(String id) {
        AnalysisProcessVo analysisProcess = analysisProcessMapper.selectByPrimaryKey(id);

        return analysisProcess;
    }

    /**
     * 根据问题ID查询分析处理任务
     *
     * @param problemId
     * @return
     */
    @Override
    public List<AnalysisProcessVo> findVoListByProblemId(String problemId) {
        List<AnalysisProcessVo> list = analysisProcessMapper.findVoListByProblemId(problemId);

        return list;
    }

    /**
     * 根据任务id查询下一节点任务标识
     *
     * @param pipiid
     * @return
     */
    public String nextTkiid(EmployeeVo employeeVo, String pipiid) {
        String nextTkiid = "";
        try {
            Thread.sleep(1000);
            System.out.print("线程睡眠1秒！\n");
            nextTkiid = ibmbpmService.queryBPMByPiid(employeeVo, pipiid);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return nextTkiid;
    }

    /**
     * 根据当前登录人信息和分析处理的流程实例ID获得当前登录人的待办
     *
     * @param employeeVo
     * @param redisValue
     * @param flowId
     * @return
     */
    @Override
    public TaskLink findTaskLinkByUserInfoFlowId(EmployeeVo employeeVo, RedisValue redisValue, String flowId) {
        String username = redisValue.getLoginName();
        String password = redisValue.getPassWord();

        TaskLink taskLink = new TaskLink();
        String result = ibmbpmService.queryToDoTask(employeeVo, username, password, "", flowId);
        if ("".equals(result)) {
            logger.info("根据流程实例ID查询待办任务无返回值！");
        } else {
            JSONObject resultObj = JSONObject.parseObject(result);
            //获取data属性的值
            JSONObject dataObj = (JSONObject) resultObj.get("data");
            //获取items属性的值
            JSONArray itemsArray = (JSONArray) dataObj.get("items");

            //待办唯一：
            if (itemsArray != null && itemsArray.size() == 1) {
                JSONObject jsonObject = (JSONObject) itemsArray.get(0);

                String piPiid = (String) jsonObject.get("PI_PIID");
                String tkiid = (String) jsonObject.get("TKIID");
                String taskLinkName = (String) jsonObject.get("NAME");
                String role = (String) jsonObject.get("ASSIGNED_TO_ROLE_DISPLAY_NAME");

                taskLink.setPiPiid(piPiid);
                taskLink.setTkiid(tkiid);
                taskLink.setTaskLinkName(taskLinkName);
                taskLink.setRole(role);
            }
        }

        return taskLink;
    }

    /**
     * 判断当前登录人是否可以处理分析处理任务
     *
     * @param employeeVo
     * @param redisValue
     * @param flowId
     * @return
     */
    @Override
    public Boolean checkCanDispose(EmployeeVo employeeVo, RedisValue redisValue, String flowId) {
        String username = redisValue.getLoginName();
        String password = redisValue.getPassWord();

        Boolean flag = false;
        String result = ibmbpmService.queryToDoTask(employeeVo, username, password, "", flowId);
        if ("".equals(result)) {
            logger.info("根据流程实例ID查询待办任务无返回值！");
        } else {
            JSONObject resultObj = JSONObject.parseObject(result);
            //获取data属性的值
            JSONObject dataObj = (JSONObject) resultObj.get("data");
            //获取items属性的值
            JSONArray itemsArray = (JSONArray) dataObj.get("items");

            //待办唯一：
            if (itemsArray != null && itemsArray.size() == 1) {
                flag = true;
            }
        }

        return flag;

    }
}
